package com.example.zbh.lessona.face;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.zbh.lessona.R;
import com.example.zbh.lessona.adapter.FaceAdapter;

/**
 * Created by zbh on 15/11/26.
 */
public class FaceActivity extends Activity implements FaceAdapter.OnitemClickListener {
    private int[] faces = {R.mipmap.ee_1, R.mipmap.ee_2, R.mipmap.ee_3, R.mipmap.ee_4, R.mipmap.ee_5, R.mipmap.ee_6,
            R.mipmap.ee_7, R.mipmap.ee_8, R.mipmap.ee_9, R.mipmap.ee_10, R.mipmap.ee_11, R.mipmap.ee_12, R.mipmap.ee_13,
            R.mipmap.ee_14, R.mipmap.ee_15, R.mipmap.ee_16, R.mipmap.ee_17, R.mipmap.ee_18, R.mipmap.ee_19, R.mipmap.ee_20,
            R.mipmap.ee_21, R.mipmap.ee_22, R.mipmap.ee_23, R.mipmap.ee_24, R.mipmap.ee_25, R.mipmap.ee_26, R.mipmap.ee_27,};
    private RecyclerView recyclerView;
    private GridLayoutManager manager;
    private FaceAdapter faceAdapter;
    private EditText editFace;
    private TextView textView;
    private String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face);
        init();

    }

    private void init() {
        manager = new GridLayoutManager(this, 1, LinearLayoutManager.HORIZONTAL,false);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(manager);
        faceAdapter = new FaceAdapter();
        recyclerView.setAdapter(faceAdapter);
        faceAdapter.addData(faces);
        editFace = (EditText) findViewById(R.id.editFace);
        textView = (TextView) findViewById(R.id.textView);
        faceAdapter.setOnitemClickListener(this);


    }

    @Override
    public void OnitemClick(int position) {
        Html.ImageGetter imageGetter = new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                int id = Integer.parseInt(source);
                Drawable drawable = getResources().getDrawable(id);
                drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
                return drawable;
            }
        };
        CharSequence cs = Html.fromHtml("<img src='"+ faces[position]+ "'/>",imageGetter, null);
        editFace.getText().append(cs);
        content = FilterHtml(Html.toHtml(editFace.getText()));
        Html.ImageGetter imageGetter1 = new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                int id = Integer.parseInt(source);
                Drawable drawable = getResources().getDrawable(id);
                drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
                return drawable;
            }
        };
        CharSequence cs1 = Html.fromHtml(content,imageGetter1, null);
        textView.setText(cs1);



    }

    public String FilterHtml(String str) {
        str = str.replaceAll("<(?!br|img)[^>]+>", "").trim();
        return str;
    }

//    public static String UnicodeToGBK2(String s) {
//        String[] k = s.split(";");
//        String rs = "";
//        for (int i = 0; i < k.length; i++) {
//            int strIndex = k.indexOf("&#");
//            String newstr = k;
//            if (strIndex > -1) {
//                String kstr = "";
//                if (strIndex > 0) {
//                    kstr = newstr.substring(0, strIndex);
//                    rs += kstr;
//                    newstr = newstr.substring(strIndex);
//                }
//                int m = Integer.parseInt(newstr.replace("&#", ""));
//                char c = (char) m;
//                rs += c;
//            } else {
//                rs += k;
//            }
//        }
//        return rs;
//    }


}
